Skip to main content

Working with AQL

Learn more about the query language in the AQL documentation.

Run an AQL query

First, we need to create some documents with the name “Homer” in the collection firstCollection:

for (int i = 0; i < 10; i++) {
BaseDocument value = new BaseDocument(String.valueOf(i));
value.addAttribute("name", "Homer");
collection.insertDocument(value);
}

Get all documents with the name “Homer” from the collection firstCollection and iterate over the result:

String query = "FOR t IN firstCollection FILTER t.name == @name RETURN t";
Map<String, Object> bindVars = Collections.singletonMap("name", "Homer");
System.out.println("Executing read query ...");
ArangoCursor<BaseDocument> cursor = db.query(query, bindVars, null, BaseDocument.class);
cursor.forEach(aDocument -> System.out.println("Key: " + aDocument.getKey()));

After executing this program, the console output should look something like this:

Key: 1
Key: 0
Key: 5
Key: 3
Key: 4
Key: 9
Key: 2
Key: 7
Key: 8
Key: 6

Some details you should know about the code:

  • the AQL query uses the placeholder @name which has to be bound to a value
  • query() executes the defined query and returns a ArangoCursor with the given class (here: BaseDocument)
  • the order is not guaranteed

Delete via AQL

Now we will delete the document created before:

String query = "FOR t IN firstCollection FILTER t.name == @name "
+ "REMOVE t IN firstCollection LET removed = OLD RETURN removed";
Map<String, Object> bindVars = Collections.singletonMap("name", "Homer");
System.out.println("Executing delete query ...");
ArangoCursor<BaseDocument> cursor = db.query(query, bindVars, null, BaseDocument.class);
cursor.forEach(aDocument -> System.out.println("Removed document " + aDocument.getKey()));

After executing this program, the console output should look something like this:

Removed document: 1
Removed document: 0
Removed document: 5
Removed document: 3
Removed document: 4
Removed document: 9
Removed document: 2
Removed document: 7
Removed document: 8
Removed document: 6
 
Help us improve

Anything unclear or buggy in this tutorial? Provide Feedback